package br.usp.icmc.biomac.client.view.impl;

import java.util.List;

import br.usp.icmc.biomac.client.view.ImageAnnotation;
import br.usp.icmc.biomac.client.view.ROIAnnotationCellTable;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.TemplateAIMTemplate;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.TemplateContainerAIMTemplate;
import br.usp.icmc.dilvan.aimTemplateViewer.client.impl.ComponentsAIMImpl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ImageAnnotationImpl extends DialogBox implements ImageAnnotation {

	private static final Binder binder = GWT.create(Binder.class);

	@UiField
	TabLayoutPanel Tabs;
	@UiField
	Button closeButton;

	@UiField
	Label annotationName;
	@UiField
	Label roi;
	@UiField
	Label serieValue;
	@UiField
	Label studyDate;

	@UiField
	ListBox containersList;
	@UiField
	VerticalPanel componentsPanel;
	@UiField
	ListBox templatesList;

	@UiField
	ScrollPanel scrollPanel;

	ROIAnnotationCellTable roisTable;

	Presenter presenter;

	interface Binder extends UiBinder<Widget, ImageAnnotationImpl> {
	}

	public ImageAnnotationImpl(ROIAnnotationCellTable roisTable) {
		this.roisTable = roisTable;
		setWidget(binder.createAndBindUi(this));
		Tabs.selectTab(1);
		this.setModal(false);
		this.center();
		this.hide();
		scrollPanel.add(roisTable);
		setTitle("Annotation Window");
		containersList.setEnabled(false);
		templatesList.setEnabled(false);
	}


	@Override
	public Widget asWidget() {
		return this;
	}
	
	@Override
	public TabLayoutPanel getTabs() {
		return Tabs;
	}
	
	@Override
	public void updateTemplatesContainers() {
		templatesList.clear();
		componentsPanel.clear();
		this.presenter.getTemplateContainer();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		roisTable.setPresenter((br.usp.icmc.biomac.client.view.ROIAnnotationCellTable.Presenter) presenter);
	}

	@Override
	public void setTemplatesContainers(
			List<TemplateContainerAIMTemplate> containers) {
		this.containersList.clear();
		this.templatesList.setEnabled(false);
		this.containersList.setEnabled(true);
		this.containersList.addItem("Select a Container", "");
		for (int i = 0; i < containers.size(); i++) {
			this.containersList.addItem(containers.get(i).getNameFile(),
					containers.get(i).getNameFile());
		}

	}

	@Override
	public void setTemplates(List<TemplateAIMTemplate> templates) {
		this.templatesList.clear();
		this.templatesList.setEnabled(true);
		this.templatesList.addItem("Select a Template", "");
		for (int i = 0; i < templates.size(); i++) {
			this.templatesList.addItem(templates.get(i).getCodeMeaning() + " ("
					+ templates.get(i).getCodeValue() + ")", templates.get(i)
					.getName());
		}
	}

	@Override
	public void createForm(TemplateAIMTemplate template) {
		componentsPanel.clear();
		ComponentsAIMImpl comp = new ComponentsAIMImpl();
		// connect with the presenter 
		comp.setPresenter((br.usp.icmc.dilvan.aimTemplateViewer.client.ComponentsAIM.Presenter) presenter);
		// create the form and join the another parameters 
		comp.createForm("imageReferenceUID", "roiID", "templateContainerID", "templateID", "userID", template);
		// add the components in the panel
		componentsPanel.add(comp);
	}

	@Override
	public void setROI(ROI roi) {
		this.annotationName.setText(roi.getLabel());
		this.roi.setText(roi.getLabel());
		this.serieValue.setText(roi.getLabel());
		this.studyDate.setText(roi.getDateTime().toString());
		containersList.clear();
		componentsPanel.clear();
	}

	@Override
	public void showAnnotation() {
		this.center();
		this.show();
		roisTable.redraw();
	}

	@Override
	public ROIAnnotationCellTable getROIAnnotationCellTable() {
		return roisTable;
	}

	@UiHandler("closeButton")
	void onCloseButtonClick(ClickEvent event) {
		this.hide();
		Tabs.selectTab(1);
	}

	@UiHandler("containersList")
	void onContainersListChange(ChangeEvent event) {
		presenter.getTemplates(containersList.getValue(containersList.getSelectedIndex()));
	}

	@UiHandler("templatesList")
	void onTemplatesListChange(ChangeEvent event) {
		presenter.getTemplate(
				containersList.getItemText(containersList.getSelectedIndex()),
				templatesList.getValue(templatesList.getSelectedIndex()));
	}
}
